package demo;

import com.detu.InputUtil;

import java.util.List;
import java.util.Scanner;

public class Demo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("1:注册   2:登录   3:修改  4:查询所有对象");
        int a = sc.nextInt();
        switch (a){
            case 1:
                regUser();//测试注册功能
                break;
            case 2:
                while (true){
                    int num =loginUser();
                    if(num==1){
                        break;
                    }
                };
                break;
            case 3:
                updateUser();break;
            case 4:
                selectAll();break;
            default:
                System.out.println("输入错误");//测试登录功能

        }
    }
    public static int loginUser() {
        UserLoginDTO dto = InputUtil.getInputObject(new UserLoginDTO(),"欢迎登录");
        System.out.println(dto);
        UserMapper userMapper = new UserMapper();
        User user = userMapper.selectByUsername(dto.getUsername());
        System.out.println(user);

        if (user == null){
            System.out.println("用户不存在");
            return 0;
        }else if (!user.getPassword().equals(dto.getPassword())){
            System.out.println("密码错误");
            return 0;
        }else {
            System.out.println("登录成功，欢迎回来"+user.getNickname());
            return 1;
        }
    }
    public static void regUser() {
        User user = InputUtil.getInputObject(new User(),"欢迎注册");
        UserMapper userMapper = new UserMapper();
        if(userMapper.selectByUsername(user.getUsername())==null){
            int num =userMapper.insert(user);
            System.out.println(num>0?"注册成功":"注册失败");
        }else {
            System.out.println("用户已存在");
        }

    }
    public static void updateUser(){
        UserMapper userMapper = new UserMapper();
        int num1 = loginUser();
        if(num1==1){
            UserUpdate user = InputUtil.getInputObject(new UserUpdate(),"修改用户信息");
            int num = userMapper.update(user);
            System.out.println(num>0?"修改成功":"修改失败");
        }else {
            System.out.println("用户不存在");
        }
    }
    public static void selectAll(){
        UserMapper userMapper = new UserMapper();
        List<User> list = userMapper.selectAll();
        for (User user:list){
            System.out.println(user);
        }
    }
}
